//===============================
//  功能 ：备注 
//  作者 ：上中野辅亚瑟王 
//  创建时间 ：2024-08-21  11-00-21 
//  Unity版本  ：2019.4.35f1 
//  变更时间 :  2024-08-21  11-00-21 
//===============================





using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(SerializedMonoBehaviour),true)]
public class DicPropertyDrawer : Editor
{
    private SerializedMonoBehaviour script { get { return target as SerializedMonoBehaviour; } }

    private Dictionary<string, DictionaryDrawer> _dictionaryDrawersCache = new Dictionary<string, DictionaryDrawer>();
    private Dictionary<string, DictionaryDrawer> _dictionaryDrawers = new Dictionary<string, DictionaryDrawer>();

    public override void OnInspectorGUI()
    {

        //先绘制Unity 默认的绘制
        DrawDefaultInspector();

        _dictionaryDrawersCache.Clear();
        foreach (var item in _dictionaryDrawers)
        {
            _dictionaryDrawersCache.Add(item.Key,item.Value);
        }
        _dictionaryDrawers.Clear();


        // 绘制一个自定义的标题
        EditorGUILayout.LabelField("My Custom Component", EditorStyles.boldLabel);

        FieldInfo[] fieldInfos = target.GetType().GetFields();
        
        foreach (var fieldInfo in fieldInfos)
        {
            //判断是否为字典类型
            if (fieldInfo?.FieldType.IsGenericType == true && fieldInfo.FieldType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
            {
                //检测字典类型是否包含特性 DicAttribute
                var dicAttribute = fieldInfo.GetCustomAttribute<DicAttribute>();
                if (dicAttribute == null)
                    continue;

                //获取字典的泛型参数类型
                Type[] genericArgs = fieldInfo.FieldType.GetGenericArguments();

                if (genericArgs.Length == 2)
                {
                    Type keyType = genericArgs[0];
                    Type valueType = genericArgs[1];

                    //_dictionaryDrawers.Add(new DictionaryDrawer());

                    if (_dictionaryDrawersCache.ContainsKey($"{fieldInfo.Name}{keyType.Name}{valueType.Name}"))
                    {
                        _dictionaryDrawers.Add($"{fieldInfo.Name}{keyType.Name}{valueType.Name}", _dictionaryDrawersCache[$"{fieldInfo.Name}{keyType.Name}{valueType.Name}"]);
                    }
                    else
                    {
                        _dictionaryDrawers.Add($"{fieldInfo.Name}{keyType.Name}{valueType.Name}",new DictionaryDrawer(fieldInfo));
                    }
                }

                //DicTitle(fieldInfo);

                #region
                //EditorGUILayout.BeginHorizontal();
                //{
                //    HorizontalBox(p =>
                //    {
                //        EditorGUILayout.LabelField($"{dicAttribute.keyLable}", MidBlodLable, p);

                //    },GUILayout.MaxWidth(100));


                //    HorizontalBox(p =>
                //    {
                //        EditorGUILayout.LabelField($"{dicAttribute.valueLable}", MidBlodLable,p);

                //    }, GUILayout.ExpandWidth(true));

                //    //EditorGUILayout.LabelField($"{dicAttribute.keyLable}", CenterLableInBoxStyle, GUILayout.Width(80));

                //    //EditorGUILayout.LabelField($"{dicAttribute.valueLable}", CenterLableInBoxStyle, GUILayout.ExpandWidth(true));

                //    EditorGUILayout.EndHorizontal();
                //}




                //EditorGUILayout.BeginVertical(HelpBox);

                //EditorGUILayout.IntField(5);

                //HorizontalBox(()=> {
                //    EditorGUILayout.LabelField("DDD", MidBlodLable);
                //});



                //EditorGUILayout.EndVertical();


                //GUILayout.Box(Resources.Load<Texture2D>("资源 2"));
                //EditorGUILayout.IntField(5, HelpBox);
                //EditorGUILayout.Space(-5);

                //GUIStyle style = new GUIStyle();
                //style.border = new RectOffset(5, 5, 5, 5); // 设置边框宽度  
                //style.normal.textColor = Color.red; // 边框颜色（注意：这实际上设置的是文本颜色，对于边框颜色，你可能需要依赖background Texture）  
                //// 注意：上面的textColor设置对于边框颜色没有直接影响，仅作为示例  
                //style.normal.background= Resources.Load<Texture2D>("资源 2");
                //GUILayout.Space(10);
                //GUILayout.Box( "这是一个带边框的盒子", style);
                //GUILayout.Space(10);

                //EditorGUILayout.BeginHorizontal();
                //{

                //    GUILayout.Box($"{dicAttribute.keyLable}", CenterLableInBoxStyle, GUILayout.Width(80));

                //    GUILayout.Box($"{dicAttribute.valueLable}", CenterLableInBoxStyle);

                //    EditorGUILayout.EndHorizontal();
                //}


                //EditorGUILayout.BeginVertical( );
                //{
                //    EditorGUILayout.BeginHorizontal(GUILayout.ExpandHeight(true));
                //    {
                //        EditorGUILayout.BeginHorizontal(EditorStyles.helpBox, GUILayout.ExpandHeight(true));
                //        {
                //            EditorGUILayout.LabelField($"{fieldInfo.Name}");

                //            EditorGUILayout.LabelField($"Empty", GUILayout.Width(40));

                //            EditorGUILayout.EndHorizontal();
                //        }

                //        GUILayout.Button(" + ", EditorStyles.helpBox, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(true));

                //        EditorGUILayout.EndHorizontal();
                //    }

                //    EditorGUILayout.BeginHorizontal();
                //    {



                //        EditorGUILayout.LabelField($"{dicAttribute.keyLable}", CenterLableInBoxStyle, GUILayout.Width(80));

                //        EditorGUILayout.LabelField($"{dicAttribute.valueLable}", CenterLableInBoxStyle, GUILayout.ExpandWidth(true));

                //        EditorGUILayout.EndHorizontal();
                //    }
                //    EditorGUILayout.EndVertical();
                //}
                #endregion

            }

        }

        foreach (var item in _dictionaryDrawers)
        {
            item.Value.Title();
        }

    }


    public class DictionaryDrawer
    {
        private bool isShow = false;

        private FieldInfo info;

        private DicAttribute dicAttribute;

        public DictionaryDrawer(FieldInfo fieldInfo) {
            info = fieldInfo;
            dicAttribute = fieldInfo.GetCustomAttribute<DicAttribute>();
        }

        public void Title() 
        {
            //Title
            EditorUtility.HorizontalBox(p=> {
                if (GUILayout.Button(isShow?"true":"false",EditorStyles.boldLabel))
                {
                    isShow = !isShow;
                    //Debug.Log($"isShow:   {isShow}");
                }
            });

            //
            if (isShow)
            {
                EditorGUILayout.BeginHorizontal();
                {
                    EditorUtility.HorizontalBox(p =>
                    {
                        EditorGUILayout.LabelField($"{dicAttribute.keyLable}", EditorUtility.MidBlodLable, p);

                    }, GUILayout.MaxWidth(100));


                    EditorUtility.HorizontalBox(p =>
                    {
                        EditorGUILayout.LabelField($"{dicAttribute.valueLable}", EditorUtility.MidBlodLable, p);

                    }, GUILayout.ExpandWidth(true));

                    //EditorGUILayout.LabelField($"{dicAttribute.keyLable}", CenterLableInBoxStyle, GUILayout.Width(80));

                    //EditorGUILayout.LabelField($"{dicAttribute.valueLable}", CenterLableInBoxStyle, GUILayout.ExpandWidth(true));

                    EditorGUILayout.EndHorizontal();
                }
            }
        }

    }

    #region

    private object GetValue(FieldInfo fieldInfo) 
    {
        return fieldInfo.GetValue(target);
    }

    private GUIStyle CenterLableInBoxStyle 
    {
        get {
            GUIStyle style = new GUIStyle();
            style.alignment = TextAnchor.MiddleCenter;
            style.fontStyle = FontStyle.Bold;
            style.fontSize = 14;
            style.padding = new RectOffset(5,5,5,5);
            style.normal.textColor =Color.white;
            style.border = new RectOffset(1,1,1,1);
            style.normal.background = Resources.Load<Texture2D>("资源 2");
            //style.padding = new RectOffset(0,0,0,0);
            return style;
        }
    }

    private GUIStyle MidLable 
    {
        get 
        {
            GUIStyle style = new GUIStyle(EditorStyles.label);
            style.alignment = TextAnchor.MiddleCenter;
            return style;
        }
    }

    private GUIStyle MidBlodLable
    {
        get
        {
            GUIStyle style = new GUIStyle(EditorStyles.boldLabel);
            style.alignment = TextAnchor.MiddleCenter;
            return style;
        }
    }


    private GUIStyle HelpBox 
    {
        get 
        {
            GUIStyle style = new GUIStyle(EditorStyles.helpBox);

            style.padding = new RectOffset(8,8,6,6);
            style.margin = new RectOffset(-1,-1,-1,-1);//调整间距

            return style;
        }
    }


    private void HorizontalBox(Action<GUILayoutOption[]> action,params GUILayoutOption[] options)
    {
        EditorGUILayout.BeginHorizontal(HelpBox, options);

        action?.Invoke(options);

        EditorGUILayout.EndHorizontal();
    }

    private void VeticalBox(Action action, params GUILayoutOption[] options) 
    {
        EditorGUILayout.BeginVertical(HelpBox, options);

        action?.Invoke();

        EditorGUILayout.EndVertical();
    }

    

    private void DicTitle(FieldInfo fieldInfo)
    {
        HorizontalBox(delegate {
            if (GUILayout.Button($"{fieldInfo.Name}", EditorStyles.boldLabel))
            {
                Debug.Log("DDD");
            }
        });
    }

    #endregion

}

public static class EditorUtility 
{
    public static GUIStyle MidLable
    {
        get
        {
            GUIStyle style = new GUIStyle(EditorStyles.label);
            style.alignment = TextAnchor.MiddleCenter;
            return style;
        }
    }

    public static GUIStyle MidBlodLable
    {
        get
        {
            GUIStyle style = new GUIStyle(EditorStyles.boldLabel);
            style.alignment = TextAnchor.MiddleCenter;
            return style;
        }
    }


    public static GUIStyle Box
    {
        get
        {
            GUIStyle style = new GUIStyle(EditorStyles.helpBox);

            style.padding = new RectOffset(8, 8, 6, 6);
            style.margin = new RectOffset(-1, -1, -1, -1);//调整间距

            return style;
        }
    }


    public static void HorizontalBox(Action<GUILayoutOption[]> action, params GUILayoutOption[] options)
    {
        EditorGUILayout.BeginHorizontal(Box, options);

        action?.Invoke(options);

        EditorGUILayout.EndHorizontal();
    }

    public static void VeticalBox(Action action, params GUILayoutOption[] options)
    {
        EditorGUILayout.BeginVertical(Box, options);

        action?.Invoke();

        EditorGUILayout.EndVertical();
    }
} 
